package cn.tsmchina.one.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.service.user.CustomerService;
import cn.tsmchina.one.service.user.UserManageService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.LoginUtil;
import cn.tsmchina.one.util.Return;

/**
 * Description:
 * @author zhaoying 
 * @date 2013-10-4
 * @version 1.0
 * Copyright: Copyright (c) 2013 TSM,Inc
 */
@Controller
@RequestMapping("/userManage")
public class UserManageController {
	@Autowired
	private UserManageService userManageService;
	@Autowired
	private CustomerService customerService;
	
	/**
	 * 用户信息修改
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String editUserInfo(@PathVariable("id") Long id,HttpServletRequest request)
	{	
		if(null != id){
			User user = userManageService.editUserInfo(id);
			if(null != user)
				request.setAttribute("u", user);
			else
				request.setAttribute("u", "");
		}
		
		return "userManage/editUser";
	}
	
	@RequestMapping(value="/edit",method= RequestMethod.POST)
	public String editUserInfo(@ModelAttribute User user,HttpServletRequest request)
	{
		try
		{	
			User update = userManageService.getUser(user.getId());
			update.setCredits(user.getCredits());
			userManageService.updateUser(update);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "编辑成功!");
			request.setAttribute("return",request.getContextPath()+"/userManage/query");
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "编辑失败!");
			request.setAttribute("return",request.getContextPath()+"/userManage/query");
		}
		
		return Return.redirect(request);
	}
	
	/**
	 * 用户信息查询
	 * @author  zhaoying
	 * @date 2013-8-30 下午2:06:21
	 * @return
	 */
	@RequestMapping("/query")
	protected String userManage(Model model,HttpServletRequest request)
	{
		try
		{	
			int page = 1;
			String curPage = request.getParameter("page");
			if(!StringUtils.isEmpty(curPage)){
				page = Integer.valueOf(curPage);
			}
			long cid = LoginUtil.getCustomerId(request, customerService);
			System.err.println(cid);
			List<User> list = userManageService.getUsers(page, cid);
			System.err.println(list.size());
			int total = userManageService.getTotal(cid);
			System.err.println(total);
			int pageTotal = total % C.Paging.SIZE == 0 ? total / C.Paging.SIZE : (total / C.Paging.SIZE) + 1;
			model.addAttribute("total", total);//总记录数
			model.addAttribute("pageTotal", pageTotal);//总页数
			model.addAttribute("page",page);//当前页
			model.addAttribute("users", list);
			model.addAttribute("len", list.size());
			
			return "userManage/queryUser";
		} catch (Exception e)
		{
			model.addAttribute("len", 0);
		}
		
		return null;
		
	}
}

